package com.xinchuang.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinchuang.comment.R;
import com.xinchuang.entity.User;
import com.xinchuang.entity.train.TrainRecord;
import com.xinchuang.mapper.UserMapper;
import com.xinchuang.service.RankingService;
import com.xinchuang.service.train.TrainRecordService;
import com.xinchuang.util.FileElseUtil;
import com.xinchuang.utils.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import com.alibaba.fastjson.JSON;

@Service
public class RankingServiceImpl implements RankingService {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private TrainRecordService trainRecordService;

    @Autowired
    private UserMapper userMapper;

    DecimalFormat decimalFormat = new DecimalFormat("#.00");

    @Override
    public String rankingList(JSONObject jsonObject) {
        //jsonobject解析获取页码
        String pageNum = jsonObject.getString("pageNum");
        //jsonobject解析获取页容
        String pageSize = jsonObject.getString("pageSize");
        //jsonobject解析获取日期
        JSONArray date = jsonObject.getJSONArray("date");
        //jsonobject解析获取排序
        String sortord = jsonObject.getString("sortord");

        //sortord 0,1    0为由高到底，1为由低到高
        LambdaQueryWrapper<TrainRecord> recordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(CollectionUtil.isNotEmpty(date)){
//            String date_range = "";
//            for(int i = 0;i < date.size();i++){
//                date_range = date_range == "" ?  "'" + date.get(i) + "'" : date_range + ",'" + date.get(i) + "'";
//            }
//            recordLambdaQueryWrapper.apply("DATE_FORMAT(train_time, '%Y-%m-%d') in (" + date_range +")");
            recordLambdaQueryWrapper.between(TrainRecord::getTrainTime, date.get(0) + " 00:00:00", date.get(1) + " 23:59:59");
        }
        if("0".equals(sortord)){
            recordLambdaQueryWrapper.orderByDesc(TrainRecord::getTitleNum);
        }else{
            recordLambdaQueryWrapper.last("ORDER BY (correct_num / title_num) DESC");
        }
        Page<TrainRecord> recordPage = new Page<TrainRecord>(Long.valueOf(pageNum), Long.valueOf(pageSize));
        IPage<TrainRecord> trainRecordIPage = trainRecordService.page(recordPage, recordLambdaQueryWrapper);
        List<TrainRecord> records = trainRecordIPage.getRecords();
        if(records.size() > 0){
            JSONArray recordArray = new JSONArray();
            for(int i = 0 ; i < records.size(); i++){
                JSONObject recordDetail = new JSONObject();
                TrainRecord trainRecord = records.get(i);
                if (null != trainRecord.getCorrectNum() && null != trainRecord.getTitleNum()) {
                    if (0 == trainRecord.getCorrectNum()) {
                        recordDetail.put("accuracy", "0");
                    } else {
                        double object = FileElseUtil.getObjectToDouble(trainRecord.getCorrectNum() / Double.valueOf(trainRecord.getTitleNum()) * 100);
                        recordDetail.put("accuracy", object);
                    }
                } else {
                    recordDetail.put("accuracy", "0");
                }
                recordDetail.put("lastTime", null != trainRecord.getTrainTime() ? sdf.format(trainRecord.getTrainTime()) : "");
                recordDetail.put("number", null != trainRecord.getTitleNum() ? trainRecord.getTitleNum() : 0);
                User user = userMapper.selectById(trainRecord.getUserId());
                if (ObjectUtil.isNotEmpty(user)) {
                    recordDetail.put("xName", userMapper.selectById(trainRecord.getUserId()).getName());
                } else {
                    recordDetail.put("xName", "该用户已删除");
                }
                recordArray.add(recordDetail);
            }

            long total = trainRecordIPage.getTotal();
            JSONObject recordJson = new JSONObject();
            recordJson.put("rankingList", recordArray);
            recordJson.put("pageNum", Integer.valueOf(pageNum));
            recordJson.put("pageSize", Integer.valueOf(pageSize));
            recordJson.put("total", total);
            recordJson.put("pageAll", Math.ceil(trainRecordIPage.getTotal() * 1.0/ Long.valueOf(pageSize)));
            return JSON.toJSONString(R.data(recordJson));
        }else{
            return JSON.toJSONString(R.success("无数据"));
        }
    }

}
